package com.yerbamateprimer.advice.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.google.common.base.Preconditions;
import com.yerbamateprimer.advice.MultipleReceptorAdvice;
import com.yerbamateprimer.model.User;

/**
 * This class models an advice
 * to let several users to 
 * know that they are invited 
 * to a round by another user.
 * 
 * @author Matias Liwski
 * @version 1.0
 */
public class UsersInvitationToRoundAdvice implements MultipleReceptorAdvice {
	private static final long serialVersionUID = 7570660582596686418L;
	private List<User> receptors;
	private User invitingUser;

	/**
	 * @param receptors The list of receptors for this advice.
	 * @param invitingUser The inviting user.
	 */
	public UsersInvitationToRoundAdvice(List<User> receptors, User invitingUser) {
		this.receptors = new ArrayList<User>();
		addAllAdviceReceptors(receptors);
		setInvitingUser(invitingUser);
	}

	/* (non-Javadoc)
	 * @see com.yerbamateprimer.advice.MultipleReceptorAdvice#addReceptor(com.yerbamateprimer.model.User)
	 */
	@Override
	public void addAdviceReceptor(User adviceReceptor) {
		Preconditions.checkArgument(adviceReceptor != null, "Couldn't add a null receptor to this advice.");
		this.receptors.add(adviceReceptor);
	}

	/* (non-Javadoc)
	 * @see com.yerbamateprimer.advice.MultipleReceptorAdvice#addAllAdviceReceptors(java.util.Collection)
	 */
	@Override
	public void addAllAdviceReceptors(Collection<User> adviceReceptors) {
		Preconditions.checkArgument(adviceReceptors != null && !adviceReceptors.isEmpty(), "Couldn't add invalid receptors (receptors=" + adviceReceptors + ").");
		this.receptors.addAll(adviceReceptors);
	}

	/**
	 * A safe copy of advice receptors.
	 * @see com.yerbamateprimer.advice.MultipleReceptorAdvice#getAdviceReceptors()
	 */
	@Override
	public List<User> getAdviceReceptors() {
		return new ArrayList<User>(this.receptors);
	}

	/**
	 * @param invitingUser The inviting user to set.
	 */
	public void setInvitingUser(User invitingUser) {
		Preconditions.checkArgument(invitingUser != null, "This advice need to know who are inviting to round.");
		this.invitingUser = invitingUser;
	}

	/**
	 * @return The inviting user.
	 */
	public User getInvitingUser() {
		return invitingUser;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE, false, false);
	}
}